package com.apes.pi.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.event.EventConst;
import com.apes.framework.plugin.event.Listener;
import com.apes.pi.model.PiQueue;
import com.apes.pi.model.Pipe;
import com.apes.pi.parser.QueueCacheManager;
import com.apes.pi.repository.PiQueueRepository;
import com.apes.pi.repository.PipeParamsRepository;
import com.apes.pi.repository.PipeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 队列
 *
 * @author wuq
 * @create 2019-10-22 16:18
 */
@Service
public class PiQueueService extends DomainService {

    @Autowired
    PiQueueRepository piQueueRepository;

    @Autowired
    PipeRepository pipeRepository;

    @Autowired
    PipeParamsRepository pipeParamsRepository;

    public PiQueue create(SimpleRequest request) {
        PiQueue queue = request.getO(PiQueue.class);
        queue = piQueueRepository.saveAndFlush(queue);
        queue.setBeforePipes(request.getA("beforePipes", Pipe.class));
        queue.setAfterPipes(request.getA("afterPipes", Pipe.class));
        queue = save(queue);
        SpringManager.getBean(QueueCacheManager.class).addQueue(queue);
        return queue;
    }

    private PiQueue save(PiQueue queue) {
        String queueId = queue.getId();
        pipeRepository.deleteByParent(queueId);

        // 前置拦截器
        List<Pipe> beforePipe = queue.getBeforePipes();
        if (beforePipe != null && beforePipe.size() > 0) {
            beforePipe.stream().forEach(pipe -> {
                pipe.setParent(queueId);
                pipeRepository.saveAndFlush(pipe);
            });
        }

        // 后置拦截器
        List<Pipe> afterPipe = queue.getAfterPipes();
        if (afterPipe != null && afterPipe.size() > 0) {
            afterPipe.stream().forEach(pipe -> {
                pipe.setParent(queueId);
                pipeRepository.saveAndFlush(pipe);
            });
        }
        return piQueueRepository.findOne(queue.getId());
    }

    public PiQueue update(SimpleRequest request) {
        PiQueue queue = request.getO(PiQueue.class);
        queue = piQueueRepository.saveAndFlush(queue);
        queue.setBeforePipes(request.getA("beforePipes", Pipe.class));
        queue.setAfterPipes(request.getA("afterPipes", Pipe.class));
        queue = save(queue);
        SpringManager.getBean(QueueCacheManager.class).addQueue(queue);
        return queue;
    }

    public String delete(SimpleRequest request) {
        PiQueue queue = request.getO(PiQueue.class);
        // 删除下面的管道数据
        String queueId = queue.getId();
        pipeRepository.deleteByParent(queueId);
        // 删除队列
        piQueueRepository.delete(queue);
        SpringManager.getBean(QueueCacheManager.class).removeQueue(queue.getId());
        return "删除成功";
    }


    @Listener(topic = EventConst.POST_LOAD_EVENT)
    public void postUpdate(SimpleRequest request) {
        Object entity = request.getData();
        if (entity instanceof PiQueue) {
            PiQueue queue = (PiQueue) entity;
            String queueId = queue.getId();
            List<Pipe> pipes = pipeRepository.findByParentOrderById(queueId);

            List<Pipe> beforePipes = new ArrayList<>();
            List<Pipe> afterPipes = new ArrayList<>();

            pipes.stream().forEach(pipe -> {
                if ("before".equals(pipe.getPosition())) {
                    beforePipes.add(pipe);
                } else {
                    afterPipes.add(pipe);
                }
            });

            queue.setBeforePipes(beforePipes);
            queue.setAfterPipes(afterPipes);
        }
    }

}
